import { Component, OnInit, OnDestroy } from "@angular/core";
import { ReplaySubject } from "rxjs/ReplaySubject";
import { Subject } from "rxjs/Subject";
import { Subscription } from "rxjs/Subscription";
import { HsClientService } from "vtrade";
import { BusinessDetail } from "./business-detail";
import { BusinessSummary } from "./business-summary";
import { Observable } from "rxjs/Observable";
import * as _ from "lodash";
import * as moment from "moment";
import { REQUESTGAP } from "./../vtrade-const";

const DETAIL = 0;
const SUMMARY = 1;

export interface QueryDate {
  start: string;
  end: string;
}

@Component({
  // moduleId: module.id,
  selector: "business-today",
  templateUrl: "business-today.component.html",
  styleUrls: [
    "business-today.component.less"
  ]
})
export class BusinessTodayComponent implements OnInit {
  tabList = [
    {
      text: "当日成交明细",
      key: DETAIL
    },
    {
      text: "当日成交汇总",
      key: SUMMARY
    },
  ];
  updateSource = new Subject();
  detailSource;
  summarySource: Observable<BusinessSummary>;

  constructor(private hsClient: HsClientService) { }

  ngOnInit() {
    this.detailSource =
      Observable.merge(this.hsClient.broadcast, this.updateSource, Observable.interval(REQUESTGAP))
        .switchMap(ask => this.hsClient.queryBusiness())
        .publishReplay(1);
    this.detailSource.connect();
    this.summarySource =
      this.detailSource.map(
        (details: BusinessDetail[]) => this.getSummaries(details))

    this.updateSource.next();
  }


  /**
   * 
   * 
   * @param {BusinessDetail[]} details
   * @returns
   * 
   * @memberOf BusinessTodayComponent
   */
  getSummaries(details: BusinessDetail[]) {
    return _.values(_.groupBy(details, "stock_code")) // {000001: [BusinessDetail, BusinessDetail]}
      .map(details =>
        this.getSingleSummary(details))
      .map((summary: BusinessSummary) =>
        Object.assign({}, summary, {
          amount_diff: (+summary.buy_amount || 0) - (+summary.sell_amount || 0),
          balance_diff: (+summary.buy_balance || 0) - (+summary.sell_balance || 0)
        })
      );
  }


  /**
   * 
   * 
   * @param {BusinessDetail[]} details
   * @returns
   * 
   * @memberOf BusinessTodayComponent
   */
  getSingleSummary(details: BusinessDetail[]) {
    return _.values(_.groupBy(details, "entrust_bs")) // [[BusinessDetail, BusinessDetail, BusinessDetail, BusinessDetail], [BusinessDetail,BusinessDetail]]
      .map((bs: BusinessDetail[]) =>
        bs.reduce((acc: BusinessDetail, next: BusinessDetail) =>
          Object.assign({}, acc, {
            business_amount: +acc.business_amount + (+next.business_amount),
            business_price: ((acc.business_price * acc.business_amount + next.business_price * (+next.business_amount)) / (+acc.business_amount + (+next.business_amount))).toFixed(3),
            business_balance: +acc.business_balance + (+next.business_balance),
          })
        )
      ).reduce((acc: BusinessDetail, next: BusinessDetail) => Object.assign({}, acc, {
        stock_name: next.stock_name,
        stock_code: next.stock_code,
        [+next.entrust_bs === 1 ? "buy_price_avg" : "sell_price_avg"]: next.business_price,
        [+next.entrust_bs === 1 ? "buy_amount" : "sell_amount"]: next.business_amount,
        [+next.entrust_bs === 1 ? "buy_balance" : "sell_balance"]: next.business_balance,
      }), {});
    // .map((bs: BusinessDetail[]) =>
    //   bs.reduce((acc: BusinessDetail, next: BusinessDetail) =>
    //     Object.assign({}, acc, {
    //       occur_amount: (+acc.occur_amount) + (+next.occur_amount), // 交易量累加
    //       business_price: (acc.business_price * acc.occur_amount + next.business_price * next.occur_amount) / (+acc.occur_amount + (+next.occur_amount)), // 加权算均价
    //       business_balance: +acc.business_balance + (+next.business_balance), // 交易额累加
    //     })
    //   )
    // ).reduce((acc: BusinessDetail, next: BusinessDetail) => Object.assign({}, acc, {
    //   stock_name: next.stock_name,
    //   stock_code: next.stock_code,
    //   [+next.entrust_bs === 1 ? "buy_price_avg" : "sell_price_avg"]: next.business_price, // 根据bs分类
    //   [+next.entrust_bs === 1 ? "buy_amount" : "sell_amount"]: Math.abs(next.occur_amount),
    //   [+next.entrust_bs === 1 ? "buy_balance" : "sell_balance"]: next.business_balance,
    // }), {});
  }
}